# Create your views here.
import json
import logging
from django.db.models import Q
from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
from utils.myModelViewSet import IsAuthenticatedView
from book1.models import Word as Word1
from book2.models import Word as Word2
from book3.models import Word as Word3
from book1.serializers import WordSerializer as WordSerializer1
from book2.serializers import WordSerializer as WordSerializer2
from book3.serializers import WordSerializer as WordSerializer3

logger = logging.getLogger(__name__)


class WordViewSet(GenericViewSet):
    queryset = Word1.objects.all()
    serializer_class = WordSerializer1

    def advanced_search(self, request, pk):
        # 词条 释义 加 书名
        word1 = Word1.objects.filter(Q(word__contains=pk) | Q(file_label__contains=pk) | Q(word_explain__word__contains=pk))
        word2 = Word2.objects.filter(Q(word__contains=pk) | Q(file_label__contains=pk) | Q(word_explain__word__contains=pk))
        word3 = Word3.objects.filter(Q(word__contains=pk) | Q(file_label__contains=pk) | Q(word_explain__word__contains=pk))
        serializer1 = WordSerializer1(word1, many=True).data
        serializer2 = WordSerializer2(word2, many=True).data
        serializer3 = WordSerializer3(word3, many=True).data
        data = serializer1+serializer2+serializer3
        return Response(data)

    def common_search(self, request, pk):
        # 单个 词条查询
        word1 = Word1.objects.filter(word__contains=pk)
        word2 = Word2.objects.filter(word__contains=pk)
        word3 = Word3.objects.filter(word__contains=pk)
        serializer1 = WordSerializer1(word1, many=True).data
        serializer2 = WordSerializer2(word2, many=True).data
        serializer3 = WordSerializer3(word3, many=True).data
        data = serializer1+serializer2+serializer3
        return Response(data)


class ImageViewSet(IsAuthenticatedView):

    def get_bookName(self, request):
        book = ImageData.objects.filter(parent_id=None)
        serializer = ImageDataSerializer(book, many=True)
        return Response(serializer.data)
